home *** CD-ROM | disk | FTP | other *** search
- Path: news.tcd.net!news
- From: Steve <slavis@PW1.PrairieWeb.Com>
- Newsgroups: comp.lang.c++
- Subject: Q: Please help me to write to .dat file!
- Date: 9 Jan 1996 03:51:39 GMT
- Organization: The Computer Den, Inc. Evanston WY
- Message-ID: <4csoob$kj@news.tcd.net>
- NNTP-Posting-Host: pw1.prairieweb.com
- Mime-Version: 1.0
- Content-Type: text/plain
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
- OK, I just about have this code whipped EXCEPT for one problem.
- Here's what I'm trying to do:
- I'm trying to write binary information to a .dat file. This code is not
- designed to READ from the file, ONLY to WRITE to the .dat file.
- I am writing structures of various questions to the file.
- It first asks to "Enter question number" , I enter: 1
- It then asks "Enter answer", I would *like* to enter: B *BUT* the program
- doesn't halt to allow me to enter the answer B
- It instead skips to "Enter question", I enter: Why is the sky blue?
- Thquestion is successfully written to file.
- Why can't I enter my answer B????? Can one only enter info into the input
- stream only once or something like that?????
- Anyway, here's the code I've struggled with.
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- main()
- {
- struct cpaQuestion {
- int questionNum;
- char question[];
- char answer[];
- };
- ofstream outQuestion("cpa.dat", ios::ate);
- if(!outQuestion) {
- cerr << "File isn't going to open." << endl;
- exit(1);
- }
- cout << "Enter the question number:" << endl;
- cpaQuestion audit;
- cin >> audit.questionNum;
- if(audit.questionNum > 0 && audit.questionNum <= 100) {
- cout << "Enter answer:" <<endl;
- cin.getline(audit.answer, 3);
- }
- if(audit.questionNum > 0 && audit.questionNum <= 100) {
- cout << "Enter question:" <<endl;
- cin.getline(audit.question, 200);
- }
- outQuestion.seekp((audit.questionNum - 1) *sizeof(audit));
- outQuestion.write((char *)&audit, sizeof(audit));
- return 0;
- }
-
-